program after_shortsale_20_days
      character*6 ticker
      character*8 cusp
      integer(kind=8):: nshrs,ivol(5000),numshrs(5000)
      dimension jdate(5000),favg(5000),iperm(5000),price(5000),ishrout(5000),vwret(5000),retrn(5000),nep(5000),pctloan(5000)
      dimension chgloan(5000)
      open(unit=10,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\ret_aft_shortsale_rep.txt')
      iend=0
      j=0
1     j=j+1
      read(10,1000,end=900)nep(j),jast,jdate(j),cusp,ticker,itickt,numshrs(j),age,favg(j),fmax,fmin,fstd,iperm(j),price(j),ivol(j),retrn(j),ishrout(j),vwret(j),util,nshrs
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13)
      pctloan(j)=float(numshrs(j))/(1000.*float(ishrout(j)))
      if(nep(j).eq.nep(1)) go to 1
5     jcnt=j-1
      if(jcnt.lt.20) go to 500
      rcnt=float(jcnt)
      avgchg=0.
      ctpos=0.
      ctneg=0.
      chgmax=0.
      chgmin=999.
      retpos=0.
      retneg=0.
      ctpos2=0.
      ctneg2=0.
      retpos2=0.
      retneg2=0.
      ctpos5=0.
      ctneg5=0.
      retpos5=0.
      retneg5=0.
      ctlpos=0.
      retlpos=0.
      ctlpos2=0.
      retlpos2=0.
      ctlpos5=0.
      retlpos5=0.
      ctturn=0.
      turnpos=0.
      turnneg=0.
      turn2pos=0.
      turn2neg=0.
      turnlg=0.
      turnlpos=0.
      turnl2pos=0.
      retsdpos=0.
      retsdneg=0.
      retsdlpos=0.
!      do 10 ii=3,jcnt
      do 10 ii=4,jcnt
          jcntm3=jcnt-3
          im=ii-1
          im2=ii-2
          im3=ii-3
          ip=ii+1
          ip2=ii+2
          ip3=ii+3
          ip4=ii+4
    !
          chgloan(ii)=pctloan(ii)-pctloan(im)
          avgchg=avgchg+chgloan(ii)/(rcnt-2.)
          if(chgloan(ii).gt.0.) ctpos=ctpos+1.
          if(chgloan(ii).gt.0.02) ctlpos=ctlpos+1.
          if(chgloan(ii).lt.0.) ctneg=ctneg+1.
    !
          if(chgloan(ii).gt.0..and.jdate(ii).lt.20170322) retpos=retpos+retrn(im2)
          if(chgloan(ii).gt.0..and.jdate(ii).ge.20170322) retpos=retpos+retrn(im)
          if(chgloan(ii).gt.0..and.jdate(ii).lt.20170322) retsdpos=retsdpos+retrn(im3)
          if(chgloan(ii).gt.0..and.jdate(ii).ge.20170322) retsdpos=retsdpos+retrn(im2)
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) retlpos=retlpos+retrn(im2)
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) retlpos=retlpos+retrn(im)
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) retsdlpos=retsdlpos+retrn(im3)
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) retsdlpos=retsdlpos+retrn(im2)
          if(chgloan(ii).lt.0..and.jdate(ii).lt.20170322) retneg=retneg+retrn(im2)
          if(chgloan(ii).lt.0..and.jdate(ii).ge.20170322) retneg=retneg+retrn(im)
          if(chgloan(ii).lt.0..and.jdate(ii).lt.20170322) retsdneg=retsdneg+retrn(im3)
          if(chgloan(ii).lt.0..and.jdate(ii).ge.20170322) retsdneg=retsdneg+retrn(im2)
          if(chgloan(ii).gt.chgmax) chgmax=chgloan(ii)
          if(chgloan(ii).lt.chgmin) chgmin=chgloan(ii)
          if(chgloan(ii).gt.0.and.jdate(ii).ge.20170322) turnpos=turnpos+float(ivol(im2))/(1000.*float(ishrout(im2)))
          if(chgloan(ii).gt.0.and.jdate(ii).lt.20170322) turnpos=turnpos+float(ivol(im3))/(1000.*float(ishrout(im3)))
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) turnlpos=turnlpos+float(ivol(im2))/(1000.*float(ishrout(im2)))
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) turnlpos=turnlpos+float(ivol(im3))/(1000.*float(ishrout(im3)))
          if(chgloan(ii).lt.0.and.jdate(ii).ge.20170322) turnneg=turnneg+float(ivol(im2))/(1000.*float(ishrout(im2)))
          if(chgloan(ii).lt.0.and.jdate(ii).lt.20170322) turnneg=turnneg+float(ivol(im3))/(1000.*float(ishrout(im3)))
          if(chgloan(ii).gt.0.and.jdate(ii).ge.20170322) turn2pos=turn2pos+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).gt.0.and.jdate(ii).lt.20170322) turn2pos=turn2pos+float(ivol(im2))/(1000.*float(ishrout(im2)))
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) turnl2pos=turnl2pos+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) turnl2pos=turnl2pos+float(ivol(im2))/(1000.*float(ishrout(im2)))
          if(chgloan(ii).lt.0.and.jdate(ii).ge.20170322) turn2neg=turn2neg+float(ivol(im))/(1000.*float(ishrout(im)))
          if(chgloan(ii).lt.0.and.jdate(ii).lt.20170322) turn2neg=turn2neg+float(ivol(im2))/(1000.*float(ishrout(im2)))
    !      if(ii.eq.jcnt) go to 10
          if(chgloan(ii).gt.0.) ctpos2=ctpos2+1.
          if(chgloan(ii).gt.0.02) ctlpos2=ctlpos2+1.
          if(chgloan(ii).lt.0.) ctneg2=ctneg2+1.
    !
          if(chgloan(ii).gt.0..and.jdate(ii).lt.20170322) retpos2=retpos2+(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) retlpos2=retlpos2+(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).lt.0..and.jdate(ii).lt.20170322) retneg2=retneg2+(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0..and.jdate(ii).ge.20170322) retpos2=retpos2+(1.+retrn(ii))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) retlpos2=retlpos2+(1.+retrn(ii))*(1.+retrn(im))-1.
          if(chgloan(ii).lt.0..and.jdate(ii).ge.20170322) retneg2=retneg2+(1.+retrn(ii))*(1.+retrn(im))-1.
          if(ii.gt.jcntm3) go to 10
          if(chgloan(ii).gt.0.) ctpos5=ctpos5+1.
          if(chgloan(ii).gt.0.02) ctlpos5=ctlpos5+1.
          if(chgloan(ii).lt.0.) ctneg5=ctneg5+1.
    !
          if(chgloan(ii).gt.0..and.jdate(ii).lt.20170322) retpos5=retpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0.02.and.jdate(ii).lt.20170322) retlpos5=retlpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).lt.0..and.jdate(ii).lt.20170322) retneg5=retneg5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(im2))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0..and.jdate(ii).ge.20170322) retpos5=retpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(im))-1.
          if(chgloan(ii).gt.0.02.and.jdate(ii).ge.20170322) retlpos5=retlpos5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(im))-1.
          if(chgloan(ii).lt.0..and.jdate(ii).ge.20170322) retneg5=retneg5+(1.+retrn(ii))*(1.+retrn(ip))*(1.+retrn(ip2))*(1.+retrn(ip3))*(1.+retrn(im))-1.
10    continue 
      if(ctpos.lt.1.) go to 15
      retpos=retpos/ctpos
      turnpos=turnpos/ctpos
      turn2pos=turn2pos/ctpos
      retsdpos=retsdpos/ctpos
15    if(ctneg.lt.1.) go to 20
      retneg=retneg/ctneg
      turnneg=turnneg/ctneg
      turn2neg=turn2neg/ctneg
      retsdneg=retsdneg/ctneg
20    continue 
      if(ctpos2.lt.1.) go to 25
      retpos2=retpos2/ctpos2
25    if(ctneg2.lt.1.) go to 30      
      retneg2=retneg2/ctneg2
30    continue  
      if(ctpos5.lt.1.) go to 35
      retpos5=retpos5/ctpos5
35    if(ctneg5.lt.1.) go to 40      
      retneg5=retneg5/ctneg5
40    continue      
      
      if(ctlpos.lt.1.) go to 50
      retlpos=retlpos/ctlpos
      turnlpos=turnlpos/ctlpos
      turnl2pos=turnl2pos/ctlpos
      retsdlpos=retsdlpos/ctlpos
50    continue 
      if(ctlpos2.lt.1.) go to 60
      retlpos2=retlpos2/ctlpos2
60    continue  
      if(ctlpos5.lt.1.) go to 70
      retlpos5=retlpos5/ctlpos5
70    continue      


      write(6,9900)nep(1),rcnt,ctpos,ctneg,chgmax,chgmin,avgchg,retpos,retneg,ctpos2,retpos2,ctneg2,retneg2,ctpos5,retpos5,ctneg5,retneg5,ctlpos,retlpos,ctlpos2,retlpos2,ctlpos5,retlpos5,turnpos,turn2pos,turnneg,turn2neg,turnlpos,turnl2pos,retsdpos,retsdneg,retsdlpos
      write(99,9900)nep(1),rcnt,ctpos,ctneg,chgmax,chgmin,avgchg,retpos,retneg,ctpos2,retpos2,ctneg2,retneg2,ctpos5,retpos5,ctneg5,retneg5,ctlpos,retlpos,ctlpos2,retlpos2,ctlpos5,retlpos5,turnpos,turn2pos,turnneg,turn2neg,turnlpos,turnl2pos,retsdpos,retsdneg,retsdlpos
9900  format(1x,i4,3(1x,f5.0),5(1x,f9.6),7(1x,f5.0,1x,f9.6),9(1x,f9.6))      
    

500   continue 
      nep(1)=nep(j)
      jdate(1)=jdate(j)
      numshrs(1)=numshrs(j)
      favg(1)=favg(j)
      iperm(1)=iperm(j)
      price(1)=price(j)
      ivol(1)=ivol(j)
      retrn(1)=retrn(j)
      ishrout(1)=ishrout(j)
      vwret(1)=vwret(j)
      j=1
      go to 1
900   continue    
      iend=iend+1
      if(iend.eq.1) go to 5
    end program after_shortsale_20_days